package cn.dengqiang.basic.util;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.nio.file.Paths;
import java.util.Objects;

public class CompressUtils {


    /**
     * 从输入流中获取zip文件，并解压到指定文件
     * @param inputStream  zip文件输入流
     * @param destDir 解压后输出路径
     * @throws IOException IO异常，抛出给调用者处理
     */
    public static void unzip(InputStream inputStream, String destDir) throws IOException{
        try ( BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
              ArchiveInputStream in  = new ZipArchiveInputStream(bufferedInputStream);
              ){
            ArchiveEntry entry = null;
            while ((Objects.nonNull(entry = in.getNextEntry()))){
                if (in.canReadEntryData(entry)){
                    File file = Paths.get(destDir, entry.getName()).toFile();
                    if (entry.isDirectory()){
                        if (!file.exists()){
                            file.mkdirs();
                        }
                    }else {
                        try (OutputStream outputStream = new FileOutputStream(file);
                        ){
                            IOUtils.copy(in,outputStream);
                        }
                    }
                }else {
                    System.out.println(entry.getName());
                }
            }
        }
    }

    /**
     * 读取文件的字节数组
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(File file) throws IOException{
        File f = file;
        if (!f.exists()){
            throw new FileNotFoundException("file not exists");
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] bytes = new byte[buf_size];
            int len = 0;
            while (-1 !=(len = in.read(bytes, 0, buf_size))){
                bos.write(bytes, 0, len);
            }
            return bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }

    /**
     * 删除文件夹
     * @param folderPath
     */
    public static void delFolder(String folderPath){
        try {
            delAllFile(folderPath);
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void delAllFile(String filePathAndName) {
        try {
            String filePath = filePathAndName;
            filePath = filePath.toString();
            java.io.File myDelFile = new java.io.File(filePath);
            myDelFile.delete();
        } catch (Exception e) {
            System.out.println("删除文件操作出错");
            e.printStackTrace();
        }
    }

}
